const router = require("koa-router")()

router.prefix("/indexpage")

const {CtxBody, PageSize} = require("../models/config")

const hotSearch = require("../models/HotSearch")

var { Goods, GoodsMain, GoodsGuide, GoodsFirstClass, GoodsSecondClass } = require("../models/goods")

const { GM, FoodGM, SelfDone, ManBing, FamilyBing } = require("../models/dangan")



// 小程序前台热搜展示
router.post("/showHotSearch", async ctx => {
    let res = await hotSearch.find({ status: "1" }, "_id name status").limit(4)
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 小程序前台海报展示

router.post("/showBanner", async ctx => {
    let res = await Banner.find({ $and: [{status: "1", type: "1"}] }, "_id url link status type")
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 小程序前台静态海报展示

router.post("/showStaticBanner", async ctx => {
    let res = await Banner.find({ $and: [{status: "1", type: "2"}] }, "_id url link status type")
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 小程序前台菜单展示

router.post("/showMenu", async ctx => {
    let res = await Menu.find({ status: "1" }, "_id url link status name").limit(10)
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 分类展示

router.post("/ShowGoodsClass", async ctx => {
    let res = await GoodsSecondClass.find({ status: "1"}).limit( PageSize )
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 前台获取所有视频

 router.post("/getAllGoods", async ctx => {
     let res = await Goods.find()
     if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
 })

//  根据ID 查找商品

router.post("/FindGoodsByID", async ctx => {
    let { id } = ctx.request.body 
    if (id) {
        let res = await Goods.findOne( { _id: id })
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 根据二级分类 来查找商品

router.post("/FindGoodsBySecondClassName", async ctx => {
    let { secondName, page } = ctx.request.body
    let SkipPage = page ? page : 1
    let allCount = await Goods.find({ beLong: secondName })
    let allGoods = await Goods.find({ beLong: secondName }, "_id title stock status price sale proportion").skip((SkipPage - 1) * PageSize).limit( PageSize )
    let total = allCount.length
    if (allCount && allGoods) {
        CtxBody(ctx, 200, { allGoods, total})
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取慢性病

router.post("/getManBing", async ctx => {
    let res = await ManBing.find({ status: "1"})
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取家族病

router.post("/getFamilyBing", async ctx => {
    let res = await FamilyBing.find({ status: "1"})
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取过敏

router.post("/getGM", async ctx => {
    let res = await GM.find({ status: "1"})
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})


// 获取食物过敏

router.post("/getFoodGM", async ctx => {
    let res = await FoodGM.find({ status: "1"})
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取个人习惯

router.post("/getSelfDone", async ctx => {
    let res = await SelfDone.find({ status: "1"})
    if (res) {
        CtxBody(ctx, 200, res)
    } else {
        CtxBody(ctx, 404, "")
    }
})

// 获取所有病

router.post("/getAllBing", async ctx => {
    let gm = await GM.find({status: "1"})
    let foodgm = await FoodGM.find({status: "1"})
    let man = await ManBing.find({status: "1"})
    let family = await FamilyBing.find({status: "1"})
    let selfdone = await SelfDone.find({status: "1"})
    CtxBody(ctx, 200, {gm,foodgm, man,family,selfdone})
})

module.exports = router